﻿// RankingSejmowy 1.0 - Polish parliment statistics for the masses.
// Copyright (C) 2010 Jarosław Dubrownik
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
using System;
using System.Diagnostics;
using RankingSejmowy.BLL.Import.Parser;
using RankingSejmowy.BLL.Import.Parser.PageParsers;
using RankingSejmowy.BLL.Import.Spider;
using RankingSejmowy.Common.Localization;
using RankingSejmowy.Common.Logging;
using RankingSejmowy.Model;

namespace RankingSejmowy.BLL.Import
{
    /// <summary>
    /// Default implementation of parliment data provider. It uses http://orka.sejm.gov.pl/SQL.nsf/ as a data source.
    /// </summary>
    public class Parliment : IParliment
    {
        private readonly string CADENCE_URL = "http://orka.sejm.gov.pl/SQL.nsf/posglos?OpenAgent&{0}";
        private readonly IMeetingListParser _meetingListParser;
        private readonly IProgressLog _progressLog;
        private readonly ISiteMapParser _siteMapParser;
        private readonly ISpider _spider;

        public Parliment(ISpider spider, ISiteMapParser siteMapParser, IMeetingListParser meetingListParser,
                         IProgressLog progressLog)
        {
            _spider = spider;
            _siteMapParser = siteMapParser;
            _meetingListParser = meetingListParser;
            _progressLog = progressLog;
        }

        #region IParliment Members

        /// <summary>
        /// Gets parliment data for specify cadence.
        /// </summary>
        /// <param name="setup">Fetch settings.</param>
        /// <param name="progressLog"></param>
        /// <returns>Returns Cadence object.</returns>
        public Cadence FetchCadence(FetchSetup setup)
        {
            Debug.Assert(setup != null);
            Debug.Assert(setup.Cadence > 0);

            var url = string.Format(CADENCE_URL, setup.Cadence);
            var siteMap = _spider.GetSiteMap(new Uri(url), setup);
            _progressLog.ReportStatus(string.Format(AppStrings.ProcessingCadenceVotingFmt, setup.LastMeeting + 1,
                                                    setup.Cadence));
            var cadence = _siteMapParser.Parse(siteMap);

            Debug.Assert(cadence != null);

            return cadence;
        }

        public Meeting[] FetchMeetingList(int cadenceNum)
        {
            Debug.Assert(cadenceNum > 0);

            var url = string.Format(CADENCE_URL, cadenceNum);
            var page = _spider.GetSinglePage(new Uri(url), false);
            return _meetingListParser.Parse(page);
        }

        #endregion
    }
}